﻿<%@ Page Title="" Language="C#" MasterPageFile="~/NestedMasterPageSimple.master" %>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      BindGVCart("query", string.Empty, 1);
    }
  }

  protected void BindGVCart(string method, string SmasCode, int quantity)
  {
    ShoppingCart userWishlistCart = Session["WishlistCart"] as ShoppingCart;
    if (userWishlistCart != null)
    {
      switch (method)
      {
        case "remove":
          userWishlistCart.RemoveItem(SmasCode);
          break;
        case "update":
          userWishlistCart.UpdateItem(SmasCode, quantity);
          break;
      }
      GVCart.DataSource = userWishlistCart.CartItems;
      GVCart.DataBind();

      //GVCart.Caption = "我的預選清單 <span class='red-text'>(" + userWishlistCart.GetItemCount().ToString() + ")</span>";
      GVCart.Caption = "我的預選清單";

      //if (userWishlistCart.GetTotal() == 0)
      //{
      //  lbl_SCTotal.Text = "小計   $ " + userWishlistCart.GetTotal().ToString();
      //}
      //else
      //{
      //  lbl_SCTotal.Text = "小計    " + userWishlistCart.GetTotal().ToString("$ #,###,###");
      //}

      if (userWishlistCart.GetItemCount() > 0)
      {
        lb_AddToCart.Visible = true;
      }
      else
      {
        lb_AddToCart.Visible = false;
      }

      Label myCartCounter = Master.Master.FindControl("myWishCounter") as Label;
      myCartCounter.Text = userWishlistCart.GetItemCount().ToString();
    }
    else
    {
      GVCart.DataBind();
      GVCart.Caption = "我的預選清單";
      //GVCart.Caption = "我的預選清單 <span class='red-text'>(0)</span>";
      //lbl_SCTotal.Text = "小計   $ 0";
      lb_AddToCart.Visible = false;
    }
  }

  protected void GVCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    Label lbl_SMAS_CODE = GVCart.Rows[e.RowIndex].FindControl("lbl_SMAS_CODE") as Label;
    BindGVCart("remove", lbl_SMAS_CODE.Text, 1);
  }

  

  protected void GVCart_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      Label lbl_ProdCatID = e.Row.FindControl("lbl_ProdCatID") as Label;
      Label lbl_B2C_CODE = e.Row.FindControl("lbl_B2C_CODE") as Label;

      ImageButton img = e.Row.FindControl("img_Path") as ImageButton;
      img.PostBackUrl = "~/Products/ProductDetail.aspx?B2C_CODE=" + lbl_B2C_CODE.Text;

      LinkButton lb_ProductName = e.Row.FindControl("lb_ProductName") as LinkButton;
      lb_ProductName.PostBackUrl = "~/Products/ProductDetail.aspx?B2C_CODE=" + lbl_B2C_CODE.Text;

      //SOCIAL SHARE
      string shareUrl = ConfigurationManager.AppSettings["ShareUrl"].ToString() + lbl_B2C_CODE.Text;
      HtmlAnchor button_facebook = e.Row.FindControl("button_facebook") as HtmlAnchor;
      button_facebook.HRef =
        "javascript: void(window.open('http://www.facebook.com/share.php?u='.concat(encodeURIComponent('" + shareUrl + "')) ));";

      HtmlAnchor button_twitter = e.Row.FindControl("button_twitter") as HtmlAnchor;
      button_twitter.HRef =
        "javascript: void(window.open('http://twitter.com/share?url='.concat(encodeURIComponent('" + shareUrl + "')) .concat('&text=') .concat(encodeURIComponent(" + lb_ProductName.Text + "))));";

      HtmlAnchor button_plurk = e.Row.FindControl("button_plurk") as HtmlAnchor;
      button_plurk.HRef =
        "javascript: void(window.open('http://www.plurk.com/?qualifier=shares&amp;status=' .concat(encodeURIComponent('" + shareUrl + "')) .concat(' ') .concat('(') .concat(encodeURIComponent(" + lb_ProductName.Text + ")) .concat(')')));";

      HtmlAnchor button_sinaweibo = e.Row.FindControl("button_sinaweibo") as HtmlAnchor;
      button_sinaweibo.HRef =
        "javascript: void(window.open('http://v.t.sina.com.cn/share/share.php?title='.concat(encodeURIComponent('" + lb_ProductName.Text + "')).concat('&url=').concat(encodeURIComponent(" + shareUrl + "))));";

      //補貨中商品不能加入購物車
      Label lbl_Status = e.Row.FindControl("lbl_Status") as Label;
      if (lbl_Status.Text=="補貨中")
      {
        CheckBox cb_item = e.Row.FindControl("cb_item") as CheckBox;
        cb_item.Visible = false;
      }
    }
  }

  protected void lb_AddToCart_Click(object sender, EventArgs e)
  {
    ShoppingCart userWishlistCart = Session["WishlistCart"] as ShoppingCart;
    ShoppingCart userShoppingCart = null;
    foreach (GridViewRow row in GVCart.Rows)
    {
      CheckBox cb_item = row.FindControl("cb_item") as CheckBox;
      if (cb_item.Checked)
      {
        Label lbl_SMAS_CODE = row.FindControl("lbl_SMAS_CODE") as Label;
        var cartItem = userWishlistCart
          .CartItems
          .Where(c => c.SMAS_CODE == lbl_SMAS_CODE.Text)
          .SingleOrDefault();
        if (HttpContext.Current.Session["ShoppingCart"] == null)
        {
          userShoppingCart = new ShoppingCart();
          userShoppingCart.Created = DateTime.Now;
          userShoppingCart.AddToCart(cartItem);
          userShoppingCart.LastUpdated = DateTime.Now;
          HttpContext.Current.Session["ShoppingCart"] = userShoppingCart;
        }
        else
        {
          userShoppingCart = HttpContext.Current.Session["ShoppingCart"] as ShoppingCart;
          userShoppingCart.AddToCart(cartItem);
          userShoppingCart.LastUpdated = DateTime.Now;
        }
        //預選清單商品加入購物車後就在預選清單內清除
        BindGVCart("remove", lbl_SMAS_CODE.Text, 1);        
      }
    }
    if (userShoppingCart != null)
    {
      Label myCartCounter = Master.Master.FindControl("myCartCounter") as Label;
      myCartCounter.Text = userShoppingCart.GetItemCount().ToString();
      lbl_massage.Text = "商品加入購物車完成!!!";      
    }
    if (userWishlistCart!=null)
    {
      Label myWishCounter = Master.Master.FindControl("myWishCounter") as Label;
      myWishCounter.Text = userWishlistCart.GetItemCount().ToString();      
    }
  }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="NestedContent" runat="Server">
  <div class="list-table wish">
    
    <asp:GridView ID="GVCart" runat="server" AutoGenerateColumns="False" GridLines="None" 
      EmptyDataText="目前預選清單中沒有任何商品項目!" 
      OnRowDeleting="GVCart_RowDeleting"       
      OnRowDataBound="GVCart_RowDataBound">
      <Columns>
        <asp:BoundField HeaderStyle-CssClass="spacer">
          <HeaderStyle CssClass="spacer"></HeaderStyle>
        </asp:BoundField>
        <asp:TemplateField HeaderText="&lt;span class='select-all'&gt;全 選&lt;/span&gt;">
          <ItemTemplate>
            <asp:CheckBox ID="cb_item" runat="server" />
          </ItemTemplate>
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="t-a-c" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="商品圖">
          <ItemTemplate>
            <asp:ImageButton ID="img_Path" runat="server" Width="50px" ImageUrl='<%# Eval("ImagePath") %>' />
            <asp:Label ID="lbl_ItemId" runat="server" Text='<%# Server.HtmlEncode(Eval("ItemId").ToString()) %>' Visible="false" />
            <asp:Label ID="lbl_SMAS_CODE" runat="server" Text='<%# Server.HtmlEncode(Eval("SMAS_CODE").ToString()) %>' Visible="false" />
            <asp:Label ID="lbl_B2C_CODE" runat="server" Text='<%# Server.HtmlEncode(Eval("B2C_CODE").ToString()) %>' Visible="false" />
            <asp:Label ID="lbl_ProdCatID" runat="server" Text='<%# Server.HtmlEncode(Eval("ProdCatID").ToString()) %>' Visible="false" />
            <asp:Label ID="lbl_StorageQty" runat="server" Text='<%# Server.HtmlEncode(Eval("StorageQty").ToString()) %>' Visible="false" />
          </ItemTemplate>
          <HeaderStyle/>
          <ItemStyle Width="50px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="商品名稱">
          <ItemTemplate>
            <asp:LinkButton ID="lb_ProductName" runat="server" Text='<%# Server.HtmlEncode(Eval("ProductName").ToString()) %>'></asp:LinkButton>            
            <%# ((dynamic)Eval("Promotion")).ToString() %>
          </ItemTemplate>
          <ItemStyle Width="370px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="金額">
          <ItemTemplate>
            <asp:Label ID="lbl_Price" runat="server" Text='<%# Server.HtmlEncode(Eval("Price", "{0:$ #,###,###}").ToString()) %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="數量">
          <ItemTemplate>
            <asp:Label ID="lbl_Qty" runat="server" Text='<%# Server.HtmlEncode(Eval("Quantity").ToString()) %>'></asp:Label>
          </ItemTemplate>          
        </asp:TemplateField>
        <asp:TemplateField HeaderText="抵購哩程" Visible="False">
          <ItemTemplate>
            <asp:Label ID="lbl_Mileage" runat="server" Text='<%# Server.HtmlEncode(Eval("Mileage").ToString()) %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="規格">
          <ItemTemplate>
            <asp:Label ID="lbl_Spec" runat="server" Text='<%# Server.HtmlEncode(Eval("Spec").ToString()) %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="商品狀態">
          <ItemTemplate>
            <asp:Label ID="lbl_Status" runat="server" Text='<%# Server.HtmlEncode(Eval("Status").ToString()) %>'></asp:Label>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="">
          <ItemTemplate>
            <div class="heart">
              <div class="share-tools padded" style="display: none;">
                <h3>請選擇下列其一方式分享給好友</h3>
                <!-- Social Button BEGIN -->
                <div class="social-network">
                  <a id="button_facebook" class="button_facebook" runat="server" ></a>
                  <a id="button_twitter" class="button_twitter" runat="server"></a>
                  <a id="button_plurk" class="button_plurk" runat="server"></a>
                  <a id="button_sinaweibo" class="button_sinaweibo" runat="server"></a>
                </div>
              </div>
            </div>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
          <ItemStyle CssClass="l-border" />
          <ItemTemplate>
            <asp:ImageButton ID="ImageButton1" runat="server"
              CausesValidation="False" CommandName="Delete"
              ImageUrl="~/Images/Icons/Delete.png" Text="刪除" />
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
    <div class="pink-result">
      <asp:Label ID="lbl_massage" runat="server" EnableViewState="False" Font-Size="Large" CssClass="fl"></asp:Label>
      <asp:Label ID="lbl_SCTotal" runat="server" />
    </div>
  </div>
  <div class="control-links">
    <asp:LinkButton ID="lb_AddToCart" runat="server" CssClass="blue-btn fl" OnClick="lb_AddToCart_Click">
      <img src="~/Images/Icons/Car.png" runat="server" />選取商品加入購物車
    </asp:LinkButton>
    <asp:LinkButton ID="lb_Back" runat="server" Text="返回繼續選購"
      CssClass="btn fr" PostBackUrl="~/Home.aspx">返回繼續選購</asp:LinkButton>    
  </div>
</asp:Content>

